目录一、创建容器1.下载模版2.上传模板到PVE3.新建LXC容器二、修改容器设置1.修改容器配置文件2.创建钩子脚本三、启动容器四、OpenWrt基本设置1.修改root密码2.
目录 一、创建容器 1. 下载模版 2. 上传模板到PVE 3. 新建LXC容器 二、修改容器设置 三、启动容器 四、OpenWrt基本设置 五、安装常用软件(根据个人喜好选择) 1、oh-my-zsh安装 2、阿里云ddns安装设置 3、zerotier安装设置 4、Server酱安装设置
一、创建容器 1. 下载模版 这里选择使用openwrt-21.02.0-rc3版本,rootfs包官方下载地址
2. 上传模板到PVE
3. 新建LXC容器 通过SSH或者是网页端打开PVE终端,执行以下命令创建新容器
pct create 100 local:vztmpl/openwrt-21.02.0-rc3-x86-64-rootfs.tar.gz --rootfs local-lvm:4 --ostype unmanaged --hostname OpenWrt --arch amd64 --cores 2 --memory 2048 --swap 0 -net0 bridge=vmbr0,name=eth0
各参数说明: vmid:100 容器编号,可以根据需要自行设置,这里设为100,后面的相关设置会用到该编号。 local:vztmpl/openwrt-21.02.0-rc3-x86-64-rootfs.tar.gz 容器模板, local:vztmpl/ 指向 /var/lib/vz/template/cache/目录,是pve的默认模板存放目录,webUI上传的模板就存在该位置,可根据需要改为其他目录,openwrt-21.02.0-rc3-x86-64-rootfs.tar.gz为模板文件名。 --rootfs local-lvm:4 根磁盘位置,local-lvm可以根据实际情况修改为其他存储位置,4表示空间大小为4G。 --ostype unmanaged 系统类型,之后可在设置文件中修改。 --hostname OpenWrt 容器名称,之后可在设置文件中修改。 --arch amd64 系统架构,amd64 | arm64 | armhf | i386。 --cores 2 分配给容器的核心数。 --memory 2048 分配给容器的内存大小,这里是2G。 --swap 0 分配给容器的交换区大小,这里是0。 -net0 bridge=vmbr0,name=eth0 容器网络设置,这里设置网络0为容器中增加网卡eth0,桥接到主机的vmbr0接口。
二、修改容器设置 1. 修改容器配置文件 vim /etc/pve/lxc/100.conf
添加以下内容
# openwrt.common.conf是PVE自带的openwrt配置文件示例,内含一些基本设置 lxc.include: /usr/share/lxc/config/openwrt.common.conf # /dev/ppp pppoe拨号等功能需要用到 lxc.cgroup.devices.allow: c 108:0 rwm # 钩子脚本,用于添加 /dev/ppp等设备 hookscript: local:snippets/hookscript.pl # 将主机的网卡enp4s0分配给容器使用,根据自己的实际情况更改 lxc.net.1.type: phys lxc.net.1.link: enp4s0 lxc.net.1.flags: up
2. 创建钩子脚本 这里在官方示例guest-example-hookscript.pl的基础上进行修改,大家可以根据需要自行修改
mkdir /var/lib/vz/snippets cp /usr/share/pve-docs/examples/guest-example-hookscript.pl /var/lib/vz/snippets/hookscript.pl vim /var/lib/vz/snippets/hookscript.pl
在第36行可以找到以下内容
# Second phase 'post-start' will be executed after the guest # successfully started. print "$vmid started successfully.\n";
修改为
# Second phase 'post-start' will be executed after the guest # successfully started. system("lxc-device add -n $vmid /dev/ppp"); system("lxc-device add -n $vmid /dev/net/tun"); print "$vmid started successfully.\n";
三、启动容器 执行以下指令
pct start 100
由于PVE7.0 默认采用cgroupv2,缺少对系统类型为unmanaged的容器支持,会出现以下错误
run_buffer: 316 Script exited with status 1 lxc_init: 816 Failed to run lxc.hook.pre-start for container "100" __lxc_start: 2007 Failed to initialize container "100" startup for container '100' failed
解决方法是修改/usr/share/perl5/PVE/LXC/Setup.pm文件
vim /usr/share/perl5/PVE/LXC/Setup.pm
翻到最后,可以看到以下内容
sub unified_cgroupv2_support { my ($self) = @_; $self->protected_call(sub { $self->{plugin}->unified_cgroupv2_support(); }); }
修改为
sub unified_cgroupv2_support { my ($self) = @_; return if !$self->{plugin}; # unmanaged $self->protected_call(sub { $self->{plugin}->unified_cgroupv2_support(); }); }
继续执行pct start 100
指令启动容器,会出现WARN: old systemd ( container -> cgroup version.
警告,忽略就行。
如果还无法正常启动,可通过以下命令查看错误信息(日志保存于debug文件中)
lxc-start -n 100 --logfile debug --logpriority TRACE
四、OpenWrt基本设置 容器启动后,进入容器终端
lxc-attach 100
1.修改root密码 passwd
2.网络设置 vi /etc/config/network
修改如下内容(根据自己的实际情况更改)
config device option name 'br-lan' option type 'bridge' list ports 'eth0' config interface 'lan' option device 'br-lan' option proto 'static' option ipaddr '192.168.1.1' option netmask '255.255.255.0' option ip6assign '60' config interface 'wan' option device 'enp4s0' option proto 'pppoe' option username '1234567890' option password 'abcdefghijk'
重启网络和防火墙
/etc/init.d/network restart /etc/init.d/firewall restart
● 现在你应该能够上网,并能通过https://192.168.1.1打开OpenWrt页面了
五、安装常用软件(根据个人喜好选择) 为了避免一些异常情况发生,修改/etc/hosts,添加以下内容
185.199.108.133 raw.githubusercontent.com 140.82.112.4 github.com
安装一些基本软件和中文语言包
opkg update opkg install vim luci-i18n-base-zh-cn luci-i18n-firewall-zh-cn luci-i18n-opkg-zh-cn
1、oh-my-zsh安装 来源:https://github.com/felix-fly/openwrt-ohmyzsh
安装依赖
opkg install unzip zsh ca-certificates
安装
sh -c "$(wget -O- https://raw.githubusercontent.com/felix-fly/openwrt-ohmyzsh/master/install.sh)"
设为默认shell
which zsh && sed -i -- 's:/bin/ash:'`which zsh`':g' /etc/passwd
不想用时可以卸载
sh -c "$(wget -O- https://raw.githubusercontent.com/felix-fly/openwrt-ohmyzsh/master/uninstall.sh)"
2、阿里云ddns安装设置 安装ddns中文包和依赖
opkg install luci-i18n-ddns-zh-cn wget-ssl openssl-util
下载 update_aliyun_com.sh脚本
wget -O /usr/lib/ddns/update_aliyun_com.sh https://raw.githubusercontent.com/sensec/ddns-scripts_aliyun/master/update_aliyun_com.sh
3、zerotier安装设置 安装zerotier软件包
opkg install zerotier
设置
uci set zerotier.sample_config.enabled='1' # 0123456789ABCDE1替换成自己的网络ID uci set zerotier.sample_config.join='0123456789ABCDE1'
启动zerotier
/etc/init.d/zerotier start
查看状态
zerotier-cli listnetworks
显示信息类似以下格式
200 listnetworks 200 listnetworks 0123456789ABCDE1 XX:XX:XX:XX:XX:XX ACCESS_DENIED PRIVATE ztqu1d2mhx -
显示status状态是 ACCESS_DENIED,需要去ZeroTier Central设置通过。之后再执行zerotier-cli listnetworks
显示status状态是OK就说明设置成功
修改防火墙设置
vim /etc/firewall.user
添加以下内容,其中ztxxxxxxxx更改为zerotier-cli listnetworks
显示的dev项
iptables -I FORWARD -i ztxxxxxxxx -j ACCEPT iptables -I FORWARD -o ztxxxxxxxx -j ACCEPT iptables -t nat -I POSTROUTING -o ztxxxxxxxx -j MASQUERADE
重启防火墙
/etc/init.d/firewall restart
4、Server酱安装设置 使用说明:https://github.com/tty228/luci-app-serverchan/
安装依赖,因为作者没有把luci-compat加进依赖库,需要手动安装(已安装的可忽略,比如安装dockerman的时候会自动安装luci-compat)
opkg install luci-compat
下载安装软件包
wget https://github.com/tty228/luci-app-serverchan/releases/download/v1.86/luci-app-serverchan_1.86-9_all.ipk opkg install luci-app-serverchan_1.86-9_all.ipk
设置
按照提示和格式要求填写“企业微信凭证” Server酱官方配置说明文档点击这里 设置好以后,点击“定时推送”–>“发送” 可以收到下图格式的信息 其他设置可以自行摸索